<template>
  <div class="lb-agentLevel">
    <lb-title></lb-title>
    <div class="page-main search-form">
      <el-row class="page-top-operate">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="$router.push('/editAgentLevel')"
          >{{ $t('menu.AddAgentLevel') }}</el-button
        >
      </el-row>
      <!-- <div class="page-top-operate form-search">
        <el-form @submit.native.prevent :inline="true" :model="searchForm">
          <el-form-item label="代理等级">
            <el-input v-model="searchForm.title" placeholder="请输入代理等级名称"></el-input>
          </el-form-item>
          <lb-button type="primary" @click="getTableDataList(1)">{{$t('action.search')}}</lb-button>
        </el-form>
      </div> -->
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        stripe
        style="width: 100%"
      >
        <el-table-column prop="top" label="排序值"></el-table-column>
        <el-table-column prop="title" label="代理等级"></el-table-column>
        <el-table-column prop="content" label="备注"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <el-button
                type="primary"
                size="mini"
                plain
                @click="$router.push('/editAgentLevel?id=' + scope.row.id)"
                >{{ $t('action.edit') }}</el-button
              >
              <el-button
                type="danger"
                size="mini"
                plain
                @click="toDel(scope.row.id)"
                >{{ $t('action.delete') }}</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :page="searchForm.page"
        :pageSize="searchForm.page_count"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      ></lb-page>
    </div>
  </div>
</template>

<script>
export default {
  name: 'auth',
  data () {
    return {
      loading: false,
      searchForm: {
        page: 1,
        page_count: 10
        // title: ''
      },
      total: 0,
      tableData: []
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    getTableDataList () {
      this.loading = true
      let { searchForm } = this
      this.$api.agentLevelList(searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    handleSizeChange (val) {
      this.searchForm.page_count = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    handleChange (value) {
      this.searchForm.province_code = value[0] || ''
      this.searchForm.city_code = value[1] || ''
    },
    async toDel (id) {
      let confirmText = this.$t('tips.confirmDelete')
      this.$confirm(confirmText, this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.toConfirmDel(id)
      }).catch(() => {

      })
    },
    async toConfirmDel (id) {
      let { code } = await this.$api.agentLevelDel({ id })
      if (code !== 200) return
      this.$message.success(this.$t('tips.successOper'))
      this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.page_count) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.page_count)
      this.getTableDataList()
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-agentLevel {
  width: 100%;
}

.page-main .el-form .el-input {
  width: 200px;
}
</style>
